package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.search.domain.AirInfo;
import cn.wolfcode.luowowo.search.domain.City;
import cn.wolfcode.luowowo.search.domain.FlightInfo;
import cn.wolfcode.luowowo.search.service.IAirInfoService;
import cn.wolfcode.luowowo.search.service.ICityService;
import cn.wolfcode.luowowo.search.service.IFlightInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/flight")
public class FlightController {
    @Reference
    private IFlightInfoService flightInfoService;
    @Reference
    private IAirInfoService airInfoService;
    @Reference
    private ICityService cityService;



    @RequestMapping("")
    public String index(Model model){
        //hotFlights
        List<City> hotFlights = cityService.selectByIsHot();
        model.addAttribute("hotFlights",hotFlights);
        //initialA
        List<City> initialA = cityService.selectByAircodeA();
        model.addAttribute("initialA",initialA);
        //initialF
        List<City> initialF = cityService.selectByAircodeF();
        model.addAttribute("initialF",initialF);

        //initialK
        List<City> initialK = cityService.selectByAircodeK();
        model.addAttribute("initialK",initialK);

        //initialQ
        List<City> initialQ = cityService.selectByAircodeQ();
        model.addAttribute("initialQ",initialQ);
        //initialX
        List<City> initialX = cityService.selectByAircodeX();
        model.addAttribute("initialX",initialX);
        //
        return "/flight/index";
    }


    @RequestMapping("/search")
    @ResponseBody
    public AjaxResult search(Model model ,String orgCity,String dstCity,@DateTimeFormat(pattern = "yyyy-MM-dd") Date depTime){
        //通过传过来的数据查找
        FlightInfo flightInfo = flightInfoService.selectByOrgCityAndDstCity(orgCity,dstCity);
        System.out.println(flightInfo);
        //通过来往城市的id查找航班
        List<AirInfo> result = airInfoService.selectByFlightId(flightInfo.getId());
        flightInfo.setList(result);
        //返回ajax
        AjaxResult ajaxResult = new AjaxResult( );
        ajaxResult.setData(result);
        return ajaxResult;
    }
}
